package cn.com.fuhuitong.main.base

import android.os.Bundle
import cn.com.fuhuitong.base.BaseAbstractFragment
import cn.com.fuhuitong.base.BaseViewModel

abstract class LazyViewModeFragment<ViewModel : BaseViewModel> : BaseAbstractFragment() {

    protected lateinit var viewModel: ViewModel

    private var isLazyLoad = false
    private var isFirstLoad = false

    abstract fun viewModel(): ViewModel

    override fun doBeforeInit() {
        super.doBeforeInit()
        viewModel = viewModel()
        initViewMode()
    }


    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        isLazyLoad = true
        if (userVisibleHint) {
            onLazyLoad()
            isFirstLoad = false
            isLazyLoad = true
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        isFirstLoad = false
    }


    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        if (isLazyLoad && isVisibleToUser) {
            //刷新操作
            onRefreshLoad1()
        }
        if (isFirstLoad && isVisibleToUser) {
            onLazyLoad()
            isFirstLoad = false
            isLazyLoad = true
        }
    }

    open fun initViewMode() = Unit

    open fun onLazyLoad() = Unit

    open fun onRefreshLoad1() = Unit

}